package pl.pepeluk.smartgwtspring.client;

import gwtupload.client.DecoratedFileUpload;
import gwtupload.client.IFileInput;
import gwtupload.client.IFileInput.FileInputType;
import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.OnCancelUploaderHandler;
import gwtupload.client.IUploader.OnStatusChangedHandler;
import gwtupload.client.IUploader.UploadedInfo;
import gwtupload.client.IUploader.UploaderConstants;
import gwtupload.client.ModalUploadStatus;
import gwtupload.client.MultiUploader;
import gwtupload.client.PreloadedImage;
import gwtupload.client.PreloadedImage.OnLoadPreloadedImageHandler;
import gwtupload.client.SingleUploader;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FlowPanel;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Progressbar;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.UploadItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Application implements EntryPoint {
	
	 // A panel where the thumbnails of uploaded images will be shown
//	  private FlowPanel panelImages = new FlowPanel();
	IButton programButton = new IButton("Programuj");
	
	// Load the image in the document and in the case of success attach it to the viewer
	  private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
	    public void onFinish(IUploader uploader) {
	      if (uploader.getStatus() == Status.SUCCESS) {
	    	  
	    	programButton.setDisabled(false);
	    	
//	        new PreloadedImage(uploader.fileUrl(), showImage);
	        
//	        // The server sends useful information to the client by default
	        UploadedInfo info = uploader.getServerInfo();
	        System.out.println("File name " + info.name);
	        System.out.println("File content-type " + info.ctype);
	        System.out.println("File size " + info.size);
	        System.out.println("Url: " + uploader.fileUrl());
	        System.out.println(uploader.getBasename());

	        // You can send any customized message and parse it 
	        System.out.println("Server message " + info.message);
	      }
	    }
	  };

	  // Attach an image to the pictures viewer
//	  private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
//	    public void onLoad(PreloadedImage image) {
//	      image.setWidth("75px");
//	      panelImages.add(image);
//	    }
//	  };
//		
	  
	  MultiUploader defaultUploader;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		VLayout mainLayout = new VLayout();
		mainLayout.setWidth100();
		mainLayout.setWidth100();
		
		programButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
//					IFileInput fi = defaultUploader.getFileInput();
			}
		});

//		Label label = new Label();
//		label.setWidth100();
//		label.setContents("Test Upload");
//		
//		final DynamicForm form = new DynamicForm(); 
//        form.setEncoding(Encoding.MULTIPART);
//        form.setAction(GWT.getModuleBaseURL()+"fileupload");
//        
//		UploadItem fileItem = new UploadItem("binaria");
//		fileItem.setShowTitle(false);
//		
//		form.setItems(fileItem);
//
//		IButton uploadButton = new IButton("Upload...");
//		uploadButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler(){
//			public void onClick(ClickEvent e){
//				form.submitForm();
//			}
//		});
//		
//		mainLayout.addMember(label);
//		mainLayout.addMember(uploadButton);		
//		mainLayout.addMember(form);
		
		 // Attach the image viewer to the document
//	    mainLayout.addMember(panelImages);
	    
	    // Create a new uploader panel and attach it to the document
	    defaultUploader = new MultiUploader();
	    defaultUploader.setMaximumFiles(1);
		programButton.setDisabled(true);
//	    SingleUploader defaultUploader = new SingleUploader();
//	    SingleUploader defaultUploader = new SingleUploader(FileInputType.BROWSER_INPUT, new ModalUploadStatus());
		
		defaultUploader.addOnCancelUploadHandler(new OnCancelUploaderHandler() {
			
			public void onCancel(IUploader uploader) {
				// TODO Auto-generated method stub
				
			}
		});
		
	    mainLayout.addMember(defaultUploader);
	    mainLayout.addMember(programButton);
	    

	    // Add a finish handler which will load the image once the upload finishes
	    defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);

		mainLayout.show();
	}
}
